﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ZeroCross
{
    public class TicTacRoom : IGameRoom
    {
        private int[] FieldState { get; set; }

        private List<IPlayer> Players = new List<IPlayer>();

        private IPlayer ActivePlayer = null;

        public TicTacRoom(IPlayer player1, IPlayer player2)
        {            
            Players.Add(player1);
            Players.Add(player2);
            FieldState = new int[9];
            ActivePlayer = player1;
        }

        public string Move(string request)
        {
            string result = string.Empty;
            string[] arguments = request.Split('^');

            int TargetCell = Convert.ToInt32(arguments[2].ToString(), CultureInfo.CurrentCulture);

            if (FieldState[TargetCell] == 0)
            {
                if (ActivePlayer == Players[0])
                {
                    FieldState[TargetCell] = 1;
                    ActivePlayer = Players[1];
                }
                else
                {
                    FieldState[TargetCell] = 2;
                    ActivePlayer = Players[0];
                }

                string winner = CheckWinner(arguments[1]);
                if (winner.Length > 0)
                {
                    result = winner;
                }
                else
                {
                    result = CheckGameState(arguments[1]);
                }

            }
            else
            {
                result = "Wrong move";
            }
            return result;
        }

        public string CheckGameState(string Nickname)
        {
            string result = string.Empty;
            string winner = CheckWinner(Nickname);
            if (winner.Length > 0)
            {
                result = winner;
            }
            else
            {
                for (int i = 0; i < FieldState.Length - 1; i++)
                {
                    result += FieldState[i].ToString() + ",";
                }
                result += FieldState[8].ToString();
                result = string.Format("{0}{1}{2}{3}", "ok^", result, "^", ActivePlayer.Nickname);
            }
            return result;
        }

        private string CheckWinner(string Nickname)
        {
            string result = string.Empty;
            int win = 0;
            if (FieldState[0] == FieldState[1] && FieldState[0] == FieldState[2])
            {
                win = FieldState[0];
            }
            else if (FieldState[0] == FieldState[4] && FieldState[0] == FieldState[8])
            {
                win = FieldState[0];
            }
            else if (FieldState[0] == FieldState[3] && FieldState[0] == FieldState[6])
            {
                win = FieldState[0];
            }
            else if (FieldState[1] == FieldState[4] && FieldState[1] == FieldState[7])
            {
                win = FieldState[1];
            }
            else if (FieldState[2] == FieldState[5] && FieldState[2] == FieldState[8])
            {
                win = FieldState[2];
            }
            else if (FieldState[2] == FieldState[4] && FieldState[2] == FieldState[6])
            {
                win = FieldState[2];
            }
            else if (FieldState[3] == FieldState[4] && FieldState[3] == FieldState[5])
            {
                win = FieldState[3];
            }
            else if (FieldState[6] == FieldState[7] && FieldState[6] == FieldState[8])
            {
                win = FieldState[6];
            }
            else
            {
                bool draw = true;
                for (int i = 0; i < FieldState.Length; i++)
                {
                    if (FieldState[i] == 0)
                    {
                        draw = false;
                        break;
                    }
                }
                if (draw)
                {
                    result = "draw";
                }
            }

            if (win == 1)
            {
                result = Players[0].Nickname;
            }
            else if (win == 2)
            {
                result = Players[1].Nickname;
            }

            if (result.Length > 0)
            {
                if (result == Nickname)
                {
                    result = "ok^Victory";                    
                }
                else if (result == "draw")
                {
                    result = "ok^Draw";
                }
                else
                {
                    result = "ok^Lost";
                }
                ReleasePlayers(Nickname);
            }           

            return result;
        }

        private void ReleasePlayers(string Nickname)
        {
            if (Nickname == Players[0].Nickname)
            {
                Players[0].OnlineStatus = "online";
            }
            else
            {
                Players[1].OnlineStatus = "online";
            }
        }
    }
}
